

#include "mlink.h"
#include "app_sntp.h"
#include "mdf_info_store.h"
#include "mlink_sntp_handle.h"
#include "lk8563.h"

static const char *TAG                       = "mlink_sntp";

#define SNTP_CONFIG_STROE_KEY "sntp_config"

// static sntp_config_t sntp_config = {0, ""};

mdf_err_t sntp_load_config( sntp_config_t *config ) {
	// mdf_err_t ret = MDF_OK;
	return mdf_info_load(SNTP_CONFIG_STROE_KEY, config, sizeof(sntp_config_t));
}

mdf_err_t sntp_save_config( sntp_config_t *config ) {
	// mdf_err_t ret = MDF_OK;
	return mdf_info_save(SNTP_CONFIG_STROE_KEY, config, sizeof(sntp_config_t));
}

static mdf_err_t mlink_handle_set_sntp_config(mlink_handle_data_t *handle_data)
{
    mdf_err_t ret   = MDF_OK;
	sntp_config_t config = {0, ""};
	sntp_load_config( &config );
    int val;
    ret = mlink_json_parse(handle_data->req_data, "en", &val);
    MDF_ERROR_CHECK(ret != MDF_OK, ret, "Parse the json formatted string");
    config.enable = val;

    ret = mlink_json_parse(handle_data->req_data, "server", &config.server);
    // MDF_ERROR_CHECK(ret != MDF_OK, ret, "Parse the json formatted string");
	
	sntp_save_config(&config);
    return MDF_OK;
}

static mdf_err_t mlink_handle_get_sntp_config(mlink_handle_data_t *handle_data)
{
    mdf_err_t ret      = ESP_OK;
	sntp_config_t config = {0, ""};
	sntp_load_config( &config );
	handle_data->resp_size = mlink_json_pack(&handle_data->resp_data, "en", config.enable);
	handle_data->resp_size = mlink_json_pack(&handle_data->resp_data, "server", config.server);
    return ret;
}

static time_t stringToTimestamp(const char *timeStr) {
    struct tm tm;
    sscanf(timeStr, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
    tm.tm_year -= 1900;
    tm.tm_mon--;
    tm.tm_isdst = -1;
    return mktime(&tm);
}

static mdf_err_t mlink_handle_set_date_time(mlink_handle_data_t *handle_data)
{
    mdf_err_t ret   = MDF_OK;
	char date[32];
    ret = mlink_json_parse(handle_data->req_data, "date", date);
    MDF_ERROR_CHECK(ret != MDF_OK, ret, "Parse the json formatted string");

	struct timeval tv = {0};
	tv.tv_sec = stringToTimestamp(date);
	ret = settimeofday(&tv, NULL);

	PCF_systohc();
    return MDF_OK;
}

static mdf_err_t mlink_handle_get_date_time(mlink_handle_data_t *handle_data)
{
    mdf_err_t ret      = ESP_OK;
    time_t currentTime;
    time(&currentTime);
    struct tm *timeInfo = localtime(&currentTime);
    char formattedTime[20];
    strftime(formattedTime, sizeof(formattedTime), "%Y-%m-%d %H:%M:%S", timeInfo);
    ESP_LOGI(TAG, "%s", formattedTime);
	handle_data->resp_size = mlink_json_pack(&handle_data->resp_data, "date", formattedTime);
    return ret;
}

static mdf_err_t mlink_handle_set_sntp(mlink_handle_data_t *handle_data)
{
    mdf_err_t ret   = MDF_OK;
	// uint32_t nowtime;
	// char *server_list[SNTP_SERVER_MAX_NUM];
	// int server_num;
	
    // ret = mlink_json_parse(handle_data->req_data, "server", &server_num);
    // MDF_ERROR_CHECK(ret != MDF_OK, ret, "Parse the json formatted string");
	
    // ret = mlink_json_parse(handle_data->req_data, "server", &server_list);
    // MDF_ERROR_CHECK(ret != MDF_OK, ret, "Parse the json formatted string");

	// for(int i=0; i < server_num && i < SNTP_SERVER_MAX_NUM; ++i){
	// 	app_sntp_set_servername(i, server_list[i]);
	// 	MDF_FREE(server_list[i]);
	// }
	// sntp_stop_obtain_time();
	// sntp_start_obtain_time();

    return MDF_OK;
}

static mdf_err_t mlink_handle_get_sntp(mlink_handle_data_t *handle_data)
{
    mdf_err_t ret      = ESP_OK;

	// char *server=NULL;
	// for(int i=0; i < SNTP_SERVER_MAX_NUM; ++i){
		// mlink_json_pack(&server, "[]", app_sntp_get_servername(i));
	// }
	// handle_data->resp_size = mlink_json_pack(&handle_data->resp_data, "server", server);
    return ret;
}

mdf_err_t mlink_sntp_init(void)
{
    mdf_err_t ret      = ESP_OK;

	mlink_set_handle("set_sntp_config", mlink_handle_set_sntp_config);
    mlink_set_handle("get_sntp_config", mlink_handle_get_sntp_config);

	mlink_set_handle("set_date_time", mlink_handle_set_date_time);
	mlink_set_handle("get_date_time", mlink_handle_get_date_time);
	return ret;
}

